app.controller('BiGuangBaoCtrl', function($ionicBackdrop, $ionicConfig, $timeout, $http, $rootScope, $myconfig, $interval, $scope, $state, $ionicSideMenuDelegate, $ionicHistory, $ionicPopup, $stateParams, FetchDataService, BthSevice, PopupService, SPAService) {

    $scope.userMes = JSON.parse(localStorage.getItem('user'));
    //判定当前设备类型是ios还是android
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

    $scope.slideOpen = false; //是否打开侧边栏
    $scope.toggleLeft = function() {
        $ionicSideMenuDelegate.toggleLeft();
        $scope.slideOpen = true;
        var timer = $interval(function() { // 设置个定时器判断侧边栏是否打开，如果是关闭状态则隐藏黑色透明蒙层
            if (!$ionicSideMenuDelegate.isOpen()) {
                $scope.slideOpen = false;
                $interval.cancel(timer);
            }
        }, 200);
    };

    $scope.goCoryza = function(type, _time) {
        if ((SPAService.upKeepData.type && SPAService.upKeepData.type != type.type) || (SPAService.coryzaData.type && SPAService.coryzaData.type != type.type)) {
            $scope.typename = SPAService.title;
            var myPopup = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/isSPA.html',
                scope: $scope
            });
            $scope.closeSPATip = function() {
                myPopup.close();
            }
            return;
        }

        // $rootScope.Bgb_time = _time;
        // $rootScope.connected = true;
        // $state.go('app.Coryza', type);
        // return;

        BthSevice.isConnect(function(status) { // 先判断程序是否已经连接上
            if ($rootScope.bthshow && status) {
                $rootScope.Bgb_time = _time;
                $rootScope.connected = true;
                $state.go('app.Coryza', type);
            } else { // 提示未连接设备
                // PopupService.lanya($scope);
                var myPopup = $ionicPopup.show({
                    templateUrl: 'templates/bitongbao/module/lanya.html',
                    scope: $scope
                });
                $scope.myclose = function() {
                    myPopup.close();
                    $ionicHistory.nextViewOptions({
                        disableAnimate: true,
                        disableBack: true,
                        historyRoot: true
                    });
                    $state.go('app.MyLink');
                };

            }
        });
    };

    //默认样式定义
    $scope.downShow = {
        hidden: false
    };

    //定义样式定时隐藏
    $scope.downHidd = function() {
        $timeout(function() {
            $scope.downShow.hidden = true;
        }, 5000);
    };
    $scope.downHidd();


    /*//设备模式强度数据请求
    $scope.mode_mes = function() {
        $http.get($myconfig.api.base_host + '/device/device_info?device_id=1')
            .then(function(res) {
                for (var i = 0; i < res.data.data.mode.length; i++) {
                    res.data.data.mode[i].img_all = [];
                    res.data.data.mode[i].img_half = [];
                    res.data.data.mode[i].img_no = [];
                    if (res.data.data.mode[i].strength == 1) {
                        var a = 0,
                            b = 1,
                            c = 2;
                        res.data.data.mode[i].img_all.length = a;
                        res.data.data.mode[i].img_half.length = b;
                        res.data.data.mode[i].img_no.length = c;
                    } else if (res.data.data.mode[i].strength == 1.5) {
                        var a = 1,
                            b = 0,
                            c = 2;
                        res.data.data.mode[i].img_all.length = a;
                        res.data.data.mode[i].img_half.length = b;
                        res.data.data.mode[i].img_no.length = c;
                    } else if (res.data.data.mode[i].strength == 2 || res.data.data.mode[i].strength == 2.5) {
                        var a = 1,
                            b = 1,
                            c = 1;
                        res.data.data.mode[i].img_all.length = a;
                        res.data.data.mode[i].img_half.length = b;
                        res.data.data.mode[i].img_no.length = c;
                    } else if (res.data.data.mode[i].strength == 3 || res.data.data.mode[i].strength == 3.5) {
                        var a = 2,
                            b = 0,
                            c = 1;
                        res.data.data.mode[i].img_all.length = a;
                        res.data.data.mode[i].img_half.length = b;
                        res.data.data.mode[i].img_no.length = c;
                    } else if (res.data.data.mode[i].strength == 4 || res.data.data.mode[i].strength == 4.5) {
                        var a = 2,
                            b = 1,
                            c = 0;
                        res.data.data.mode[i].img_all.length = a;
                        res.data.data.mode[i].img_half.length = b;
                        res.data.data.mode[i].img_no.length = c;
                    } else if (res.data.data.mode[i].strength == 5) {
                        var a = 3,
                            b = 0,
                            c = 0;
                        res.data.data.mode[i].img_all.length = a;
                        res.data.data.mode[i].img_half.length = b;
                        res.data.data.mode[i].img_no.length = c;
                    }
                }
                $scope.mode_mes_data = res.data.data;
            })
    };
    // 自执行
    $scope.mode_mes();*/
    $scope.mode_mes_data = FetchDataService.getCoryzaData();

    var date_bgb = new Date();

    //当日使用次数请求
    $scope.count_BGB = function() {
        $http.get($myconfig.api.base_host + '/physicallogs/count_b?dev_name=' + '金鼻光' + '&start_time=' + new Date(date_bgb.toLocaleDateString()) + '&user_id=' + $scope.userMes.id)
            .then(function(res) {
                // console.log(res)
                $scope.count_BGB_data = parseInt(res.data.data.count);
            })
    };

    if ($scope.userMes) {
        $scope.count_BGB();
    }


    $scope.$on('$ionicView.enter', function() {

        $ionicConfig.views.swipeBackEnabled(false);

    });

});
